import csv
import re
from PyCmpltrtok.common import sep
import traceback

regex_words_in_sb = re.compile('\[([^\[\]]+)\]')


def get_en2zh_zh2en(trans_path):
    with open(trans_path, 'r', encoding="utf8") as f:
        xlines_raw = f.readlines()
    en2zh, zh2en = {}, {}
    for xline in xlines_raw:
        if xline[-2:] == '\r\n':
            xline = xline[:-2]
        elif xline[-1:] == '\n':
            xline = xline[:-1]
        xline = xline.strip()
        if not xline:
            continue
        en, zh = xline.split('>')
        en2zh[en] = zh
        zh2en[zh] = en
    return en2zh, zh2en


if '__main__' == __name__:

    trans_path = 'trans/tte_options_zh.trans.txt'
    en2zh, zh2en = get_en2zh_zh2en(trans_path)
    print('en2zh:', en2zh)
    print('zh2en:', zh2en)

    csv_path = r'D:\_dell7590_root\local\LNP_datasets\emotionX7zh\OCEMOTION.csv'
    print('path:', csv_path)

    xset_emojis = set()
    xmap_emojis_chars2cnt = {}
    xmap_emojis_chars2set = {}
    xlen_max = 0
    xset_emotions = set()
    with open(csv_path, 'r', encoding="utf8") as f:
        cnt = 0
        while True:
            xline = f.readline()
            if not xline:
                break
            if '\r\n' == xline[-2:]:
                xline = xline[:-2]
            elif '\r' == xline[-1:]:
                xline = xline[:-1]
            xline = xline.strip()
            if not xline:
                continue

            cnt += 1
            # if cnt > 10:
            #     break
            try:
                xid, xtext, xe = xline.split('\t')
                xid = int(xid)
            except Exception as ex:
                print(traceback.format_exc())

            # check emotion
            xe = xe.strip()
            xset_emotions.add(xe)

            # check emoji
            matches = re.findall(regex_words_in_sb, xtext)
            if not matches:
                continue
            print(matches)
            for xm in matches:
                xlen = len(xm)
                # max-length
                if xlen > xlen_max:
                    xlen_max = xlen
                # chars-to-cnt
                xcnt = xmap_emojis_chars2cnt.get(xlen, 0)
                xcnt += 1
                xmap_emojis_chars2cnt[xlen] = xcnt
                # chars-to-set
                xset = xmap_emojis_chars2set.get(xlen, set())
                xset.add(xm)
                xmap_emojis_chars2set[xlen] = xset

            xset_emojis = xset_emojis.union(set(matches))

    print(sorted(xset_emojis))
    sep('len2cnt')
    print('xlen_max:', xlen_max)
    for xlen in range(1, xlen_max + 1):
        xcnt = xmap_emojis_chars2cnt.get(xlen, 0)
        if not xcnt:
            continue
        print(xlen, xcnt)
    sep('check')
    for xlen in range(6, 11):
        print(xlen, xmap_emojis_chars2set.get(xlen, None))
    sep('emomtions')
    print(xset_emotions)
